package com.loveedu.services.user.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.loveedu.apib.user.dto.Pager;
import com.loveedu.mapper.SupplierMapper;
import com.loveedu.models.Supplier;
import com.loveedu.services.user.SupplierService;
import com.loveedu.utils.MD5Tools;
import com.loveedu.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018-5-28.
 */
@Service
public class SupplierServiceImpl implements SupplierService{
    @Autowired
    SupplierMapper supplierMapper;

    @Override
    public JSONObject add(Supplier supplier) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",0);
        supplier.setCreateBy(0L);
        supplier.setCreateDate(new Date());
        supplier.setEnable("0");
        supplier.setPassword(MD5Tools.getMD5(supplier.getPassword()));
        int flag = supplierMapper.insertSelective(supplier);
        jsonObject.put("msg",flag > 0?"添加供应商成功":"添加失败");
        jsonObject.put("code",flag > 0?0:-1);
        return jsonObject;
    }

    @Override
    public JSONObject list(Supplier supplier, Pager pager) {
        PageHelper.startPage(pager.getCurrentPage(), pager.getPageSize());
        List<Supplier> allItems = supplierMapper.listSupplier(supplier);
        int countNums = supplierMapper.getSupplierNum(supplier);            //总记录数
        PageBean<Supplier> pageData = new PageBean<>(pager.getCurrentPage(), pager.getPageSize(), countNums);
        pageData.setItems(allItems);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",pageData);
        jsonObject.put("code",0);
        jsonObject.put("msg","获取供应商列表成功");
        return jsonObject;
    }
}
